import { _decorator, Component, Node, UIOpacity, tween, Label, v3, Tween, Sprite } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import EventManager from '../../Init/Managers/EventManager';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';

import { v2 } from 'cc';

import Tools from '../../Init/Tools/Tools';
const { ccclass, property } = _decorator;

@ccclass('HomeUI')
export class HomeUI extends BasicUI {

    protected onLoad(): void {
        super.onLoad();
    }

    protected onEvents() {
        this.on(EventTypes.GameEvents.EnterChooseLv, this.onEnterChooseLv, this);
    }

    public show(d) {
        super.show(d);

        UISystem.hideUI(UIEnum.LevelInfoUI);
        UISystem.showUI(UIEnum.PlayerAssetsUI);

        //自动进入
        if (GlobalTmpData.UIData.isAutoEnterLv) {
            this.showLvScene();
        } else {
            AudioSystem.playBGM(AudioEnum.homeBgm);
        }

        // 自动弹出开始
        if (GlobalTmpData.UIData.isAutoStartPop) {
            GlobalTmpData.UIData.isAutoStartPop = false;
            this.enterGame();
        }

        this.setBtnInfo();

        console.log('#time2 ', new Date().getTime() - GlobalTmpData.Game.gameLaunchTime);
        // // 结束Loading动画
        EventManager.emit(EventTypes.UIEvents.ShowLoadingExitAnim);
    }

    public hide(d?: any): void {
        this.node.active = false;
        //
        UISystem.hideUI(UIEnum.SettingUI);
        UISystem.hideUI(UIEnum.NavigateToSceneUI);

    }

    public update(dt: number): void {
    }

    //进入游戏
    public enterGame() {
        this.showLvScene();
    }

    //显示场景
    public showLvScene() {
        // 切换为正式关卡
        GlobalTmpData.Game.isGuideLv = false;

        UISystem.showUI(UIEnum.LoadingUI, {
            enterCb: () => {
                this.emit(EventTypes.GameEvents.GameStart);
                console.log('#time3-1 ', new Date().getTime() - GlobalTmpData.Game.gameLaunchTime);
            }
        });
    }

    // #region -----------------按钮--------------
    @property(Node)
    startBtn: Node = null;

    protected setBtnInfo() {

    }

    /**点击开始按钮-正常模式 */
    public onGameStartClick() {
        // 检测免费次数
        const data = StorageSystem.getData();
        //默认从最大关卡开始     
        data.levelAssets.curLv = data.levelAssets.maxLv;
        StorageSystem.saveData();

        GlobalTmpData.isHardMode = false;
        console.log('闯关模式');

        this.setBtnInfo();

        this.enterGame();
    }
    /**点击开始按钮 - 挑战模式*/
    public onHardStartClick() {
        let levelAssets = StorageSystem.getData().levelAssets;
        if (levelAssets.maxLv <= GlobalConfig.UnlockHardLv) {
            EventManager.emit(EventTypes.GameEvents.ShowTips,
                '通过第' + GlobalConfig.UnlockHardLv + '关后解锁');
            return;
        }

        GlobalTmpData.isHardMode = true;
        console.log('挑战模式');

        this.enterGame();
    }

    /**显示设置UI */
    protected onShowSettingUI() {
        UISystem.showUI(UIEnum.SettingUI);
    }

    //隐私UI
    protected onShowPrivacyUI() {
        UISystem.showUI(UIEnum.PrivacyUI, { isLobby: true });
    }
    //大礼包UI
    protected onShowGiftUI() {

    }
    //签到UI
    protected onShowSignUI() {
        UISystem.showUI(UIEnum.SignUI);
    }
    //签到UI
    protected onShowTurntableUI() {
    }
    // 排行榜
    protected onRankListUIClick() {
        UISystem.showUI(UIEnum.RankPopUI);
    }
    // 分享
    protected onShareClick() {
        EventManager.emit(EventTypes.SDKEvents.Share, { title: '太好玩了,来一起挑战吧!', useUrl: true, urlType: GlobalEnum.SharePosition.HomePage })
        EventManager.emit(EventTypes.SDKEvents.ShareReport, '首页分享');
    }
    // 任务
    protected onMissionUIClick() {
        UISystem.showUI(UIEnum.MissionUI);
    }
    // #endregion    


    // #region -----------------事件------------

    //进入指定关卡
    protected onEnterChooseLv(lv: number) {
        StorageSystem.setData((d) => {
            d.levelAssets.curLv = lv;
        });
        this.enterGame();
    }


    // #endregion

}

const tmpP3_0 = v3();
const tmpP3_1 = v3();
const tmpP2_0 = v2();
const tmpP2_1 = v2();
const tmpP2_2 = v2();
